package com.hmall.search.controller;

import com.hmall.feign.dto.PageDTO;
import com.hmall.search.pojo.RequestParams;
import com.hmall.search.service.SearchService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/search")
public class SearchController {

    @Resource
    private SearchService searchService;

    /**
     * 搜索自动补全
     *
     * @param termPrefix 输入的词条前缀
     * @return 自动补全的词条集合
     */
    @GetMapping("/suggestion")
    public List<String> suggestion(@RequestParam("key") String termPrefix) throws IOException {
        return searchService.suggestion(termPrefix);
    }

    /**
     * 过滤项聚合
     * @param params
     * @return
     * @throws IOException
     */
    @PostMapping("/filters")
    public Map<String, List<String>> getFilters(@RequestBody RequestParams params) throws IOException {
        return searchService.getFilters(params);
    }

    /**
     * 基本搜索
     * @param params
     * @return
     * @throws IOException
     */
    @PostMapping("/list")
    public PageDTO search(@RequestBody RequestParams params) throws IOException {
        return searchService.search(params);
    }
}
